﻿function Save()
{
  //if(m=="") window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
  if($get("txtComment").value=="") return;
  if(__P_M==""){$Login$();return;}
  $get("btnSave").disabled = true;
  showProgressBar(true); 
  WebServices.knowledge.AddMessage1(gPageName,$get("txtComment").value,gPageSize,1,onsucc,onerror);
}
function onsucc(result)
{
    $get("btnSave").disabled = false;
    showProgressBar(false);
    if(result[0].State)
    {
        RendList(result[1],"divComment");
        RendPage(result[2],"divCommentPage",strHome,strPre,strLast,strNext,strSel,strUnSel);
        $get("commentCount").innerHTML = "("+result[3]+")";
        $get("viewCount").innerHTML = result[4];
        $get("txtComment").value = "";
        $get("MessagesBox").style.display='block';
        $get("MessageImg").src = MessageImageS;
        $get("MessageText").innerHTML = MessageTextS;
    }
    else
    {
        if(result[0].Msg =="l") window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
    }
}
function onerror(result)
{
    $get("btnSave").disabled = false;
    $get("MessagesBox").style.display='block';
    $get("MessageImg").src = MessageImageF;
    $get("MessageText").innerHTML = MessageTextF;
}
function DeleteComent(id,obj)
{
    if(m=="") window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
    obj.disabled = true;
    showProgressBar(true);
    if(gLastCount==1) gPageIndex = gPageIndex -1;
    if(gPageIndex<1) gPageIndex = 1;
    WebServices.knowledge.DeleteComent1(gPageName,id,gPageSize,gPageIndex,onDeleteSucc,onDeleteError,obj);
}
function onDeleteSucc(result,context)
{
    showProgressBar(false);
    if(result[0].State)
    {
        RendList(result[1],"divComment");
        RendPage(result[2],"divCommentPage",strHome,strPre,strLast,strNext,strSel,strUnSel);
        $get("commentCount").innerHTML = "("+result[3]+")";
        $get("viewCount").innerHTML = result[4];
        $get("MessagesBox").style.display='block';
        $get("MessageImg").src = MessageImageS;
        $get("MessageText").innerHTML = MessageTextS;
    }
    else
    {
        if(result[0].Msg =="l")
        {
            window.location = "/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
        }
        else if(result[0].Msg =="A"||result[0].Msg =="F")//A:不是管理员F:删除消息失败
        {
            context.disabled = false;
            $get("MessagesBox").style.display='block';
            $get("MessageImg").src = MessageImageF;
            $get("MessageText").innerHTML = MessageTextF;
        }
    }
}
function onDeleteError(result,context)
{
    context.disabled = false;
    $get("MessagesBox").style.display='block';
    $get("MessageImg").src = MessageImageF;
    $get("MessageText").innerHTML = MessageTextF;
}
function RendList(list,divID)
{
    var domDiv = $get(divID);
    if(domDiv!=null)
    {
        if($get("commentItem")==null) return;
        var item= $get("commentItem").innerHTML;
        var deleteItem= "";
        if($get("DeleteTool")!=null) deleteItem=$get("DeleteTool").innerHTML;
        gLastCount = list.length;
        var listBuilder = new Sys.StringBuilder();
        for (var index = 0; index < list.length; ++index) 
        {
            var obj = list[index];
            var tmp = item.replace(/\$Avatar\$/gi,"<img src=\""+(obj.Avatar==""?"/Images/No_pic.gif":obj.Avatar)+"\"/>");
            tmp = tmp.replace(/\$NickName\$/gi,obj.NickName).replace(/\$UserName\$/gi,obj.UserName).replace(/\$Body\$/gi,EncodeMessage(obj.Body));
            if(gDeleteFlag)
            {
                tmp = tmp.replace(/\$DeleteTool\$/gi,deleteItem.replace("{0}",obj.ID));
            }
            else
            {
                tmp = tmp.replace(/\$DeleteTool\$/gi,"");
            }
            var arr1 = gDateFormat.split(":");
            var arr2 = obj.DiffDate.split(":");
            if(arr2[0]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[0]).replace("{1}",arr1[0]));
            }
            else if(arr2[1]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[1]).replace("{1}",arr1[1]));
            }
            else if(arr2[2]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[2]).replace("{1}",arr1[2]));
            }
            else if(arr2[3]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[3]).replace("{1}",arr1[3]));
            }
            else if(arr2[4]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[4]).replace("{1}",arr1[4]));
            }
            else if(arr2[5]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[5]).replace("{1}",arr1[5]));
            }
            else
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,"");
            }
            listBuilder.append(tmp);
        }
        domDiv.innerHTML = listBuilder.toString();
    }
}